﻿/*
* YanSelect 选择插件
* Copyright 2011, 刘玉宏
* 2011-05-10 v1.0	编写
*/
(function ($) {
    $.fn.YanTable = function (options) {
        var defaults = {
            datasource: [],//jsondata
            columns: [],
            defaultclass: "table table-striped table-bordered table-hover",
            rowclick: $.noop

        };
        var options = $.extend(defaults, options);
        var selector = this.selector;
        var $Table = $(this);


        var Tabsort = function (x,y) {
            return (x.SortBy < y.SortBy) ? 1 : -1

        }

        var YanTable = {
            options: options,
            getrowdata:function(rowindex) {
                for (var i = 0; i <  options.datasource.length; i++) {
                    if (rowindex == i) {
                        return options.datasource[i];
                    }
                }
            },
            removetab: function () {
                $Table.empty();
            },
            loaddata: function (options) {
                options.datasource.sort(Tabsort)
                $Table.addClass(options.defaultclass);
                //添加TBHeader
                var TabHeader = "<thead><tr>";
                for (var i = 0; i < options.columns.length; i++) {
                    TabHeader = TabHeader + "<th>" + options.columns[i].text + "</th>";
                }
                TabHeader = TabHeader + "</tr></thead>";
                $Table.append(TabHeader);
                //添加TBHeader


                //添加TBHeader
                var TabBody = "<tbody class='tabData'>";
                for (var i = 0; i < options.datasource.length; i++) {
                    var TabTr = "<tr  dataid='" + options.datasource[i]["ID"] + "'>";
                    for (var m = 0; m < options.columns.length; m++) {
                        var FieldName = options.columns[m].dataField;
                        var tdvalue = options.datasource[i][FieldName];//默认为dataField对应值
                        //处理width属性
                        var width = "";
                        if (options.columns[m].hasOwnProperty('width')) {
                            width = "width:" + options.columns[m]['width'];
                        }
                        //处理width属性
                        if (options.columns[m].hasOwnProperty('ftcustom')) {
                            if ($.isFunction(options.columns[m].ftcustom)) {
                                tdvalue = "";
                                tdvalue = options.columns[m].ftcustom(options.datasource[i], i, options.datasource.length);
                            }
                        }
                        if (options.columns[m].hasOwnProperty('type')) {
                            if (options.columns[m]['type'] == 'date') {
                                tdvalue = tdvalue.replace("T", " ");
                            }
                        }
                        if (tdvalue== null) {
                            tdvalue = "";
                        }
                        TabTr = TabTr + "<td style='" + width + "'>" + tdvalue + "</td>";
                    }
                    TabTr = TabTr + "</tr>";
                    TabBody = TabBody + TabTr;
                }
                TabBody = TabBody + "</tbody><tfoot style='display:none'><tr><td >无数据</td></tr></tfoot>";
                $Table.append(TabBody);


                //单击事件
                $(".tabData tr", $Table).live('click', function () {
                    $(".tabData tr", $Table).removeClass("select");
                    $(this).addClass("select");
                    var $tr = $(this);
                    if ($.isFunction(options.rowclick)) {
                        options.rowclick.call(this, $tr);
                    }
                })
            }
        }
        YanTable.loaddata(options);

        return YanTable;
    }
})(jQuery);